package com.bjypc.youth.service.impl;

import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.transaction.annotation.Transactional;

import com.bjypc.youth.dao.IStudentDao;
import com.bjypc.youth.entity.InterpersonalRelationship;
import com.bjypc.youth.entity.LifeStyle;
import com.bjypc.youth.entity.Student;
import com.bjypc.youth.entity.StudentAssociation;
import com.bjypc.youth.entity.StudentConsume;
import com.bjypc.youth.entity.StudentFeeling;
import com.bjypc.youth.entity.StudentFutureJudgment;
import com.bjypc.youth.entity.StudentImportentLevel;
import com.bjypc.youth.entity.StudentInterest;
import com.bjypc.youth.entity.TimeUsing;
import com.bjypc.youth.service.BaseAbstractService;
import com.bjypc.youth.service.IStudentService;
import com.bjypc.youth.util.BjyouthConstants;

/**
 * AdultService
 * 
 * @author chenzwei
 * 
 */
@Transactional
public class StudentServiceImpl extends BaseAbstractService implements
		IStudentService {
	private IStudentDao studentDao;

	public Student find(Long id) {
		return this.studentDao.findById(id);
	}

	public List<Student> findAll() {
		return this.studentDao.findAll();
	}

	public void remove(Long id) {
		this.studentDao.removeById(id);

	}

	public void save(Student person) {
		this.studentDao.save(person);
	}

	public IStudentDao getStudentDao() {
		return studentDao;
	}

	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}

	public HSSFWorkbook export(List<Student> dataList) {
		HSSFWorkbook workbook = null;
		try {
			// 这里的数据即时你要从后台取得的数据

			// 创建工作簿实例
			workbook = new HSSFWorkbook();
			// 创建工作表实例
			HSSFSheet sheet = workbook.createSheet("Test Result");
			// 设置列宽
			this.setSheetColumnWidth(sheet);
			// 获取样式
			HSSFCellStyle style = this.createTitleStyle(workbook);

			//
			if (dataList != null && dataList.size() > 0) {
				// 创建第一行标题
				HSSFRow row = sheet.createRow(0);// 建立新行
				createTitleRow(row, style);

				// 给excel填充数据
				int i = 0;
				for (Student person : dataList) {
					i++;
					// 将dataList里面的数据取出来，假设这里取出来的是Model
					HSSFRow row1 = sheet.createRow(i);// 建立新行
					createTextCell(row1, 0, style,
							BjyouthConstants.SexNames[person.getSex()]);
					createTextCell(row1, 1, style, person.getBirth());
					createTextCell(row1, 2, style, person.getGrade());
					createTextCell(row1, 3, style, person.getScoreLevel());
					createTextCell(row1, 4, style, person.getLiveAddress());
					createTextCell(row1, 5, style, person.getHomeMonthIncome());
					createTextCell(row1, 6, style, BjyouthConstants
							.yesOrNot(person.isLiveInSchool()));
					createTextCell(row1, 7, style, getLiveWith(person));
					// this.createCell(row1, ++k, style,
					// HSSFCell.CELL_TYPE_STRING,
					// person.getLiveWith()+"");
					// Lifestyle，7-23
					LifeStyle lifeStyle = person.getLifeStyle();
					createNumCell(row1, 8, style, lifeStyle.getShareData());
					createNumCell(row1, 9, style, lifeStyle.getEatLessForFat());
					createNumCell(row1, 10, style, lifeStyle.getLiar());
					createNumCell(row1, 11, style, lifeStyle.getDrinkOnemore());
					createNumCell(row1, 12, style, lifeStyle.getShareLiking());
					createNumCell(row1, 13, style, lifeStyle.getHelping());
					createNumCell(row1, 14, style, lifeStyle.getCheating());
					createNumCell(row1, 15, style, lifeStyle.getSmokeMore());
					createNumCell(row1, 16, style, lifeStyle.getSurfLong());
					createNumCell(row1, 17, style, lifeStyle
							.getTeamCollaborate());
					createNumCell(row1, 18, style, lifeStyle.getJokeOthers());
					createNumCell(row1, 19, style, lifeStyle.getHelpShy());
					createNumCell(row1, 20, style, lifeStyle.getShareFeeling());
					createNumCell(row1, 21, style, lifeStyle.getDiscussEvent());
					createNumCell(row1, 22, style, lifeStyle.getCoHardTask());
					createNumCell(row1, 23, style, lifeStyle
							.getAbsentWithReason());
					createNumCell(row1, 24, style, lifeStyle
							.getHelpDifficutyStudies());
					// Relationship，25-43
					InterpersonalRelationship relationship = person
							.getInterpersonalRelationship();
					createNumCell(row1, 25, style, relationship
							.getAttractingTogether());
					createNumCell(row1, 26, style, relationship
							.getSelfIntroduce());
					createNumCell(row1, 27, style, relationship
							.getWishGoodFirstImage());
					createNumCell(row1, 28, style, relationship
							.getUnfamilierParty());
					createNumCell(row1, 29, style, relationship.getTellUnlike());
					createNumCell(row1, 30, style, relationship
							.getPersistRight());
					createNumCell(row1, 31, style, relationship
							.getTellDiscomfiture());
					createNumCell(row1, 32, style, relationship.getShowWeak());
					createNumCell(row1, 33, style, relationship
							.getTrustFrient());
					createNumCell(row1, 34, style, relationship.getTellSecret());
					createNumCell(row1, 35, style, relationship
							.getTellAppreciateLevel());
					createNumCell(row1, 36, style, relationship
							.getPatientListen());
					createNumCell(row1, 37, style, relationship.getCatchFit());
					createNumCell(row1, 38, style, relationship
							.getSupportFrient());
					createNumCell(row1, 39, style, relationship
							.getShowSincety());
					createNumCell(row1, 40, style, relationship.getNoRage());
					createNumCell(row1, 41, style, relationship
							.getListenBoring());
					createNumCell(row1, 42, style, relationship.getNoAccuse());
					createNumCell(row1, 43, style, relationship
							.getAcceptFitPoint());

					// 43-57 StudentAssociation
					StudentAssociation association = person
							.getStudentAssociation();
					createNumCell(row1, 44, style, association
							.getButtingWithTeacher());
					createNumCell(row1, 45, style, association
							.getAlignWithOthers());
					createNumCell(row1, 46, style, association
							.getTellVexToFriends());
					createNumCell(row1, 47, style, association
							.getCarefulDeclare());
					createNumCell(row1, 48, style, association
							.getLitteCommunityWithTeacher());
					createNumCell(row1, 49, style, association
							.getTeachersNoUnderstanding());
					createNumCell(row1, 50, style, association
							.getWillingCommunicateAll());
					createNumCell(row1, 51, style, association
							.getLittleRequireFromParent());
					createNumCell(row1, 52, style, association
							.getTeacherBlames());
					createNumCell(row1, 53, style, association
							.getNotCareTickets());
					createNumCell(row1, 54, style, association
							.getActiveGreeting());
					createNumCell(row1, 55, style, association
							.getOnwellWithParent());
					createNumCell(row1, 56, style, association.getMailForMiss());
					createNumCell(row1, 57, style, association
							.getInterestingWithFamilies());
					createNumCell(row1, 58, style, association
							.getCloseTiesFamily());
					createNumCell(row1, 59, style, association
							.getConcernEverySide());
					createNumCell(row1, 60, style, association
							.getUpsetLongtimeForLate());
					createNumCell(row1, 61, style, association
							.getLikeExpressViews());
					// StudentInterest，62-79
					StudentInterest interest = person.getPersonalInterest();
					createNumCell(row1, 62, style, interest
							.getLikeLearningMaths());
					createNumCell(row1, 63, style, interest
							.getLikeWorldCustoms());
					createNumCell(row1, 64, style, interest
							.getLikeActiveSpeakingEnglish());
					createNumCell(row1, 65, style, interest.getLikeMusic());
					createNumCell(row1, 66, style, interest
							.getLikePracticeLoveLong());
					createNumCell(row1, 67, style, interest.getLikeMathPuzzle());
					createNumCell(row1, 68, style, interest
							.getLikeHistoryStory());
					createNumCell(row1, 69, style, interest.getLikeWriting());
					createNumCell(row1, 70, style, interest
							.getWantKnowNatureMystery());
					createNumCell(row1, 71, style, interest
							.getLikeReadingLiteraryworks());
					createNumCell(row1, 72, style, interest.getLikePE());
					createNumCell(row1, 73, style, interest.getLikeChinese());
					createNumCell(row1, 74, style, interest
							.getRecallForeignWords());
					createNumCell(row1, 75, style, interest
							.getLikeExtraGroups());
					createNumCell(row1, 76, style, interest.getLikeDraw());
					createNumCell(row1, 77, style, interest
							.getLikeLittleInvent());
					createNumCell(row1, 78, style, interest
							.getLikeForeignClasses());
					createNumCell(row1, 79, style, interest
							.getLikeMathClasses());

					// StudentConsume 80-83
					StudentConsume consume = person.getPersonConsume();
					createTextCell(row1, 80, style,
							StudentConsume.MoneyFrom[consume.getMoneyFrom()]);
					createTextCell(row1, 81, style, consume.getMoneySpend());
					createTextCell(row1, 82, style,
							StudentConsume.BuyShoes[consume.getBuyShoes()]);
					createTextCell(row1, 83, style, consume
							.getDealWithLuckmoney());

					// TimeUsing,84-96
					TimeUsing timeUsing = person.getTimeUsing();
					createNumCell(row1, 84, style, timeUsing.getProvideHelp());
					createNumCell(row1, 85, style, timeUsing
							.getJoinCollectiveActivity());
					createNumCell(row1, 86, style, timeUsing.getSurfingOnNet());
					createNumCell(row1, 87, style, timeUsing
							.getSkateboardActitity());
					createNumCell(row1, 88, style, timeUsing.getPartTimeJob());
					createNumCell(row1, 89, style, timeUsing.getJobRelated());
					createNumCell(row1, 90, style, timeUsing.getIdling());
					createNumCell(row1, 91, style, timeUsing.getWatchTv());
					createNumCell(row1, 92, style, timeUsing.getPlayEgames());
					createNumCell(row1, 93, style, timeUsing
							.getWorkWithFamily());
					createNumCell(row1, 94, style, timeUsing
							.getPlayOtherSports());
					createNumCell(row1, 95, style, timeUsing
							.getPersonalExercise());
					createNumCell(row1, 96, style, timeUsing.getReadingBooks());

					// StudentFeeling 97-147感觉感受
					StudentFeeling feeling = person.getPersonalFeeling();
					createNumCell(row1, 97, style, feeling.getUselessEnergy());
					createNumCell(row1, 98, style, feeling
							.getDoAnythingWanted());
					createNumCell(row1, 99, style, feeling.getLikeMyself());
					createNumCell(row1, 100, style, feeling.getCreative());
					createNumCell(row1, 101, style, feeling.getTrusted());
					createNumCell(row1, 102, style, feeling.getFeelSthBad());
					createNumCell(row1, 103, style, feeling
							.getNothingInteresting());
					createNumCell(row1, 104, style, feeling.getFeelAlone());
					createNumCell(row1, 105, style, feeling.getThoughtShort());
					createNumCell(row1, 106, style, feeling.getDoNothing());
					createNumCell(row1, 107, style, feeling.getNotLoseTemper());
					createNumCell(row1, 108, style, feeling
							.getKeepGoodUnderPress());
					createNumCell(row1, 109, style, feeling
							.getKeepSmoothUnderFrust());
					createNumCell(row1, 110, style, feeling.getSelfMotivate());
					createNumCell(row1, 111, style, feeling.getGoodThinking());
					createNumCell(row1, 112, style, feeling
							.getNothingStopHappy());
					createNumCell(row1, 113, style, feeling
							.getGoodHeartFaceLife());
					createNumCell(row1, 114, style, feeling.getGoodStudyScore());
					createNumCell(row1, 115, style, feeling.getLikeStudy());
					createNumCell(row1, 116, style, feeling.getEasyWithTest());
					createNumCell(row1, 117, style, feeling.getCourseWell());
					createNumCell(row1, 118, style, feeling.getVersatility());
					createNumCell(row1, 119, style, feeling
							.getGoodatHandcraft());
					createNumCell(row1, 120, style, feeling.getGoodatDraw());
					createNumCell(row1, 121, style, feeling.getWellOnMusic());
					createNumCell(row1, 122, style, feeling.getGoodLooking());
					createNumCell(row1, 123, style, feeling.getBeatifulEye());
					createNumCell(row1, 124, style, feeling.getTall());
					createNumCell(row1, 125, style, feeling.getGoodBody());
					createNumCell(row1, 126, style, feeling.getHealthy());
					createNumCell(row1, 127, style, feeling.getLessSick());
					createNumCell(row1, 128, style, feeling.getGoodPopularity());
					createNumCell(row1, 129, style, feeling.getGetOnWell());
					createNumCell(row1, 130, style, feeling
							.getGoodTeacherShip());
					createNumCell(row1, 131, style, feeling.getWellWithParent());
					createNumCell(row1, 132, style, feeling
							.getWellWithStudents());
					createNumCell(row1, 133, style, feeling.getManyFriends());
					createNumCell(row1, 134, style, feeling.getSelfSatisfied());
					createNumCell(row1, 135, style, feeling.getSelfConfident());
					createNumCell(row1, 136, style, feeling.getSuccessWithTry());
					createNumCell(row1, 137, style, feeling.getDoAsSelfWant());
					createNumCell(row1, 138, style, feeling.getBroadLike());
					createNumCell(row1, 139, style, feeling.getLikeDancing());
					createNumCell(row1, 140, style, feeling.getLikeArt());
					createNumCell(row1, 141, style, feeling.getLeaderInGames());
					createNumCell(row1, 142, style, feeling.getNoPublicTide());
					createNumCell(row1, 143, style, feeling.getNoPublicOrder());
					createNumCell(row1, 144, style, feeling
							.getSavePublicTreasureFirst());
					createNumCell(row1, 145, style, feeling
							.getDoInterestedOnly());
					createNumCell(row1, 146, style, feeling
							.getPersonDevelopImportent());
					createNumCell(row1, 147, style, feeling.getUnitObeyGroup());
					// 148-153 重要性判断
					StudentImportentLevel importentLevel = person
							.getImportentLevel();
					createNumCell(row1, 148, style, importentLevel
							.getFreeControlSpareTime());
					createNumCell(row1, 149, style, importentLevel
							.getScoreGood());
					createNumCell(row1, 150, style, importentLevel
							.getConsiderdSmartStudent());
					createNumCell(row1, 151, style, importentLevel
							.getFreeControlMoney());
					createNumCell(row1, 152, style, importentLevel
							.getCanEntryUniversity());
					createNumCell(row1, 153, style, importentLevel
							.getMakePlan());

					// 154-159 未来判断
					StudentFutureJudgment futureJudgment = person
							.getFutureJudgment();
					createNumCell(row1, 154, style, futureJudgment
							.getCanGraduate());
					createNumCell(row1, 155, style, futureJudgment
							.getFindGoodJob());
					createNumCell(row1, 156, style, futureJudgment
							.getWorkFavorite());
					createNumCell(row1, 157, style, futureJudgment
							.getHaveHappyFamily());
					createNumCell(row1, 158, style, futureJudgment
							.getGetRespected());
					createTextCell(row1, 159, style, futureJudgment
							.getWantTodo());

				}
			} else {
				this.createCell(sheet.createRow(0), 0, style,
						HSSFCell.CELL_TYPE_STRING, "查无资料");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return workbook;

	}

	private void createNumCell(HSSFRow row, int column, HSSFCellStyle style,
			int value) {
		HSSFCell cell = row.createCell(column);
		cell.setCellStyle(style);
		cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(value);

	}

	private String getLiveWith(Student student) {
		StringBuffer liveWith = new StringBuffer();
		if (student.isLiveWithMother()) {
			liveWith.append("母亲").append("  ");
		}
		if (student.isLiveWithFather()) {
			liveWith.append("父亲").append(" ");
		}
		if (student.isLiveWithFriend()) {
			liveWith.append("男朋友或女朋友").append(" ");
		}
		if (student.isLiveWithGrand()) {
			liveWith.append("爷爷、奶奶或外公、外婆").append(" ");
		}
		if (student.isLiveWithHoney()) {
			liveWith.append("丈夫或妻子").append(" ");
		}
		if (student.isLiveWithBrother()) {
			liveWith.append("亲兄弟或继兄弟").append(" ");
		}
		if (student.isLiveWithSecondFather()) {
			liveWith.append("继父").append(" ");
		}
		if (student.isLiveWithSecondMother()) {
			liveWith.append("继母").append(" ");
		}
		if (student.isLiveWithSecondParent()) {
			liveWith.append("养父母").append(" ");
		}
		if (student.isLiveWithSister()) {
			liveWith.append("亲姐妹或继姐妹").append(" ");
		}

		return liveWith.toString();
	}

	private void createTitleRow(HSSFRow row, HSSFCellStyle style) {
		createTextCell(row, 0, style, "性别");
		createTextCell(row, 1, style, "出生年月");
		createTextCell(row, 2, style, "所在年级");
		createTextCell(row, 3, style, "学习成绩");
		createTextCell(row, 4, style, "家庭住址");
		createTextCell(row, 5, style, "家庭月收入");
		createTextCell(row, 6, style, "是否住校");
		// 和谁住在一起
		createTextCell(row, 7, style, "今年和谁住在一起");

		// 8-24
		for (int j = 0; j < LifeStyle.fieldNames.length; j++) {
			String name = LifeStyle.fieldNames[j];
			int index = 8 + j;
			createTextCell(row, index, style, index + "（二）生活方式-" + name);
		}
		// 25-43人际关系
		for (int j = 0; j < InterpersonalRelationship.fieldNames.length; j++) {
			String name = InterpersonalRelationship.fieldNames[j];
			int index = 25 + j;
			createTextCell(row, index, style, index + "（三）与人交往-" + name);
		}
		// 44-61人际关系（二）
		for (int j = 0; j < StudentAssociation.fieldNames.length; j++) {
			String name = StudentAssociation.fieldNames[j];
			int index = 44 + j;
			createTextCell(row, index, style, index + "（三）与人交往<二>-" + name);
		}

		// 62-79个人兴趣
		for (int j = 0; j < StudentInterest.fieldNames.length; j++) {
			String name = StudentInterest.fieldNames[j];
			int index = 62 + j;
			createTextCell(row, index, style, index + "（四）个人兴趣-" + name);
		}
		// 80-83 消费水平
		for (int j = 0; j < StudentConsume.fieldNames.length; j++) {
			String name = StudentConsume.fieldNames[j];
			int index = 80 + j;
			createTextCell(row, index, style, index + "（五）消费水平-" + name);
		}
		// 84-96 时间观念
		for (int j = 0; j < TimeUsing.fieldNames.length; j++) {
			String name = TimeUsing.fieldNames[j];
			int index = 84 + j;
			createTextCell(row, index, style, index + "（六）时间观念-" + name);
		}
		// 97-147感觉感受
		for (int j = 0; j < StudentFeeling.fieldNames.length; j++) {
			String name = StudentFeeling.fieldNames[j];
			int index = 97 + j;
			createTextCell(row, index, style, index + "（七）感觉感受-" + name);
		}
		// 148-153重要性判断
		for (int j = 0; j < StudentImportentLevel.fieldNames.length; j++) {
			String name = StudentImportentLevel.fieldNames[j];
			int index = 148 + j;
			createTextCell(row, index, style, index + "（八）重要性判断-" + name);
		}
		// 154-159未来判断
		for (int j = 0; j < StudentFutureJudgment.fieldNames.length; j++) {
			String name = StudentFutureJudgment.fieldNames[j];
			int index = 154 + j;
			createTextCell(row, index, style, index + "（九）未来判断-" + name);
		}

	}

	// 创建Excel单元格
	private void createCell(HSSFRow row, int column, HSSFCellStyle style,
			int cellType, Object value) {
		HSSFCell cell = row.createCell((short) column);

		if (style != null) {
			cell.setCellStyle(style);
		}
		switch (cellType) {
		case HSSFCell.CELL_TYPE_BLANK: {

		}
			break;
		case HSSFCell.CELL_TYPE_STRING: {
			cell.setCellValue(value + "");
		}
			break;
		case HSSFCell.CELL_TYPE_NUMERIC: {
			cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			// DecimalFormat format = new DecimalFormat("###,##0.00");
			// cell.setCellValue(Float.parseFloat(value.toString()));
			cell.setCellValue(Double.parseDouble(value.toString()));
		}
			break;
		default:
			break;
		}
	}

	private void createTextCell(HSSFRow row, int column, HSSFCellStyle style,
			String value) {
		HSSFCell cell = row.createCell(column);
		cell.setCellStyle(style);
		cell.setCellType(HSSFCell.CELL_TYPE_STRING);
		cell.setCellValue(new HSSFRichTextString(value));
	}

	// 设置excel的title样式
	private HSSFCellStyle createTitleStyle(HSSFWorkbook wb) {
		HSSFFont boldFont = wb.createFont();
		boldFont.setFontHeight((short) 200);
		HSSFCellStyle style = wb.createCellStyle();
		style.setFont(boldFont);
		style.setDataFormat(HSSFDataFormat.getBuiltinFormat("###,##0.00"));
		return style;
	}

	private void setSheetColumnWidth(HSSFSheet sheet) {
		// 根据你数据里面的记录有多少列，就设置多少列
		for (int i = 0; i < 7; i++) {
			sheet.setColumnWidth(i, 2000);
		}
		sheet.setColumnWidth(7, 6000);
		for (int i = 8; i < 159; i++) {
			sheet.setColumnWidth(i, 5000);
		}
	}
}
